import Cookies from 'js-cookie'
import { findTagsList ,findByParentpCateId} from '@/api/basic'
import { findTagsGroup,cateTagsGroupFindList} from '@/api/tags'

const state = {
  pager:{
    page:1,
    pageSize:15
  },
  listQuery:{
    "condition": {},
    "orderBys": [
      {
        "asc": true,
        "column": null
      }
    ],
    "pager": {
      "page": 10,
      "pageSize": 1
    }
  },
  WEB_CAST:[],
  cateTagsGroupFindList:[],
  sidebar: {
    opened: Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : true,
    withoutAnimation: false,
  },
   win:document.body.getBoundingClientRect().width,
  device: 'desktop'
}

const mutations = {
  TOGGLE_SIDEBAR: state => {
    state.sidebar.opened = !state.sidebar.opened
    state.sidebar.withoutAnimation = false
    if (state.sidebar.opened) {
      Cookies.set('sidebarStatus', 1)
    } else {
      Cookies.set('sidebarStatus', 0)
    }
  },
  RESIZEPARAM:(state,info)=>{
    console.log("win",info.win)
    state.win = info.win
  },
  CLOSE_SIDEBAR: (state, withoutAnimation) => {
    Cookies.set('sidebarStatus', 0)
    state.sidebar.opened = false
    state.sidebar.withoutAnimation = withoutAnimation
  },
  TOGGLE_DEVICE: (state, device) => {
    state.device = device
  }
}

const actions = {
  toggleSideBar({ commit }) {
    commit('TOGGLE_SIDEBAR')
  },
  resizeParam({commit,state},win){

    commit('RESIZEPARAM',win)
  },
  closeSideBar({ commit }, { withoutAnimation }) {
    commit('CLOSE_SIDEBAR', withoutAnimation)
  },
  toggleDevice({ commit }, device) {
    commit('TOGGLE_DEVICE', device)
  },
   findTagsList({commit},tagsBizEnum ='WEB_CAST') {

	return new Promise(async(resolve,reject)=>{
    const params = {
      growthClassify: '',
      stageId: '',
      subjectId: '',
      tagsBizEnum,
      tagsGroupEnum: '',
      tagsTypeEnum: 'CATEGORY_TAGS'
    }
		 let res  =await findTagsList(params)
		if(res.code ===200){
			resolve(res.data)
		}else{
      reject(res)
    }
	})
  },
  findByParentpCateId({commit},params) {
  // params.bizTypeEnum = params.bizTypeEnum  ==='TEACHING_DESIGN'?'PREPARELESSON':params.bizTypeEnum
  return new Promise( (resolve,reject)=>{
      findByParentpCateId(params).then(res=>{
        if(res.code === '1'){
        	resolve(res.data)
        }else{
          reject(res)
        }
      }).catch(()=>{
      })
  })
  },
  //多维度分类标签
  async _cateTagsGroupFindList ({state},params) {
    const res = await cateTagsGroupFindList(params)
    res.data.map((list)=>{
       list['tagsId'] = list['groupId']
       list['tagsName'] = list['groupName']
       list['sub'] = list['tagsVoList']
     return list
    })
    state[params.tagsBizEnum==='WEB_CAST'?params.tagsBizEnum:'cateTagsGroupFindList'] = res.data
  },
  findTagsGroup({commit},params) {
  return new Promise( (resolve,reject)=>{
  	 findTagsGroup(params).then(res=>{
       if(res.code === '1'){
       	resolve(res.data)
       }else{
         reject(res)
       }
     }).catch(()=>{

     })
  })
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
